package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.PKRankBean;
import com.yaozheng.vocationaltraining.bean.PKRoomBean;
import com.yaozheng.vocationaltraining.bean.SeatBean;
import com.yaozheng.vocationaltraining.fragment.exam.PKHallFragment;
import com.yaozheng.vocationaltraining.fragment.exam.PKHallFragment_;
import com.yaozheng.vocationaltraining.utils.ACache;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.view.CompareDetailDialog;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by moon on 2017/11/15.
 */

public class PKRoomAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private PKHallFragment mContext;
    private BitmapLoader userBitmapLoader;

    private List<PKRoomBean> data = null;

    private int selectedPosition = -1;

    DecimalFormat df ;

    public PKRoomAdapter(PKHallFragment context,
                         List<PKRoomBean> data) {
        this.mInflater = LayoutInflater.from(context.getActivity());
        this.mContext = context;
        this.data = data;
        df = new DecimalFormat("#.00");
        userBitmapLoader = new BitmapLoader(Glide.with(context), R.drawable.user_img);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.pk_room_gridview_item, null);
            holder = new ViewHolder();

            holder.three_player_layout = (LinearLayout) convertView.
                    findViewById(R.id.three_player_layout);

            holder.first_userAvatarImage = (CircleImageView) convertView
                    .findViewById(R.id.first_userAvatarImage);
            holder.first_name_textview = (TextView) convertView
                    .findViewById(R.id.first_name_textview);
            holder.first_win_rate_textview = (TextView) convertView
                    .findViewById(R.id.first_win_rate_textview);

            holder.second_userAvatarImage = (CircleImageView) convertView
                    .findViewById(R.id.second_userAvatarImage);
            holder.second_name_textview = (TextView) convertView
                    .findViewById(R.id.second_name_textview);
            holder.second_win_rate_textview = (TextView) convertView
                    .findViewById(R.id.second_win_rate_textview);

            holder.three_userAvatarImage = (CircleImageView) convertView
                    .findViewById(R.id.three_userAvatarImage);
            holder.three_name_textview = (TextView) convertView
                    .findViewById(R.id.three_name_textview);
            holder.three_win_rate_textview = (TextView) convertView
                    .findViewById(R.id.three_win_rate_textview);

            holder.room_Image = (CircleImageView) convertView.
                    findViewById(R.id.room_Image);
            holder.room_name_textview = (TextView) convertView.
                    findViewById(R.id.room_name_textview);


            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        if (data.get(position).getSeats().size() == 2){
            holder.three_player_layout.setVisibility(View.GONE);
        }else {
            holder.three_player_layout.setVisibility(View.VISIBLE);
        }

        holder.room_name_textview.setText("房间"+(position + 1));

        final SeatBean firstSeat = data.get(position).getSeats().get(0);
        final SeatBean secondSeat = data.get(position).getSeats().get(1);

        if (firstSeat.getPlayerId() > 0){ //表示有player
            //加载图片
            userBitmapLoader.displayImage(firstSeat.getPlayer().getAvatar(), holder.first_userAvatarImage);
            holder.first_name_textview.setText(firstSeat.getPlayer().getName());

            holder.first_win_rate_textview.setText(df.format(firstSeat.getPlayer().getWinRate() / 100.0) + "%");

            holder.first_userAvatarImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (ACache.get(mContext.getActivity()).getAsString("userName")
                            .equals(firstSeat.getPlayer().getName())){
                        mContext.openConfirmFragment(firstSeat.getRoomId(),firstSeat.getId());
                    }else {
                        mContext.compare(firstSeat.getPlayerId());
                    }
                }
            });
        }else {
            holder.first_userAvatarImage.setImageResource(R.color.default_border_color);
            holder.first_name_textview.setText("");
            holder.first_win_rate_textview.setText("");

            holder.first_userAvatarImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   // Toast.makeText(mContext.getActivity(),"席位一无人，可座下",Toast.LENGTH_SHORT).show();
                    //TODO 调用接口

                    mContext.seat(firstSeat.getId(),data.get(position).getId());
                }
            });
        }

        if (secondSeat.getPlayerId() > 0){ //表示有player
            //加载图片
            userBitmapLoader.displayImage(secondSeat.getPlayer().getAvatar(), holder.second_userAvatarImage);
            holder.second_name_textview.setText(secondSeat.getPlayer().getName());
            holder.second_win_rate_textview.setText(df.format(secondSeat.getPlayer().getWinRate() / 100.0) + "%");
            holder.second_userAvatarImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //mContext.openConfirmFragment(firstSeat.getRoomId(),firstSeat.getId());

                    if (ACache.get(mContext.getActivity()).getAsString("userName")
                            .equals(firstSeat.getPlayer().getName())){
                        mContext.openConfirmFragment(firstSeat.getRoomId(),firstSeat.getId());
                    }else {
                        mContext.compare(secondSeat.getPlayerId());
                    }
                }
            });
        }else {
            holder.second_userAvatarImage.setImageResource(R.color.default_border_color);
            holder.second_name_textview.setText("");
            holder.second_win_rate_textview.setText("");

            holder.second_userAvatarImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   // Toast.makeText(mContext.getActivity(),"席位二无人，可座下",Toast.LENGTH_SHORT).show();
                    //TODO 调用接口
                    mContext.seat(secondSeat.getId(),data.get(position).getId());
                }
            });
        }

        if (data.get(position).getSeats().size() == 3){
            final SeatBean threeSeat = data.get(position).getSeats().get(0);

            if (threeSeat.getPlayerId() > 0){ //表示有player
                //加载图片
                userBitmapLoader.displayImage(threeSeat.getPlayer().getAvatar(), holder.three_userAvatarImage);
                holder.three_name_textview.setText(threeSeat.getPlayer().getName());
                holder.three_win_rate_textview.setText(df.format(threeSeat.getPlayer().getWinRate() / 100.0) + "%");
                holder.three_userAvatarImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        if (ACache.get(mContext.getActivity()).getAsString("userName")
                                .equals(firstSeat.getPlayer().getName())){
                            mContext.openConfirmFragment(firstSeat.getRoomId(),firstSeat.getId());
                        }else {
                            mContext.compare(threeSeat.getPlayerId());
                        }
                    }
                });
            }else {
                holder.three_userAvatarImage.setImageResource(R.color.default_border_color);
                holder.three_name_textview.setText("");
                holder.three_win_rate_textview.setText("");

                holder.three_userAvatarImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       // Toast.makeText(mContext.getActivity(),"席位三无人，可座下",Toast.LENGTH_SHORT).show();
                        //TODO 调用接口
                        mContext.seat(threeSeat.getId(),data.get(position).getId());
                    }
                });
            }
        }

        return convertView;
    }

    public void setData(List<PKRoomBean> data) {
        this.data = data;
    }

    public void setselectedPosition(int selectedPosition) {
        notifyDataSetChanged();;
    }

    class ViewHolder {
        LinearLayout three_player_layout;
        CircleImageView first_userAvatarImage;
        TextView first_win_rate_textview;
        TextView first_name_textview;

        CircleImageView second_userAvatarImage;
        TextView second_win_rate_textview;
        TextView second_name_textview;

        CircleImageView three_userAvatarImage;
        TextView three_win_rate_textview;
        TextView three_name_textview;

        CircleImageView room_Image;
        TextView room_name_textview;
    }
}
